package com.jcroke.PC32.basetransform;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.LinkedHashMap;

import com.jcroke.DACGraph.Pnode;
import com.jcroke.DACGraph.Signature;
import com.jcroke.PC32.datum.Datum;
import com.jcroke.PC32.exception.TransformerException;
import com.jcroke.PC32.memory.Umem;
import com.jcroke.PC32.serialize.Record;

/**
 * 
 * @author jcroke
 *
 */
public abstract class Transformer implements Functional {
	protected LinkedHashMap<String, Datum> _iBuf;
	protected Umem _umem;
	protected Pnode _pnode;

	public Transformer() {
		_iBuf = new LinkedHashMap<String, Datum>();
	}

	protected void declareInputFields() {
		for (Signature s : _pnode.getInputSignature()) {
			_iBuf.put(
					s.getLocalPin(),
					_umem.read(Pnode.substrIRI(s.getSourcePnode()),
							s.getSourcePin()));
		}
	}

	public void emit(Hashtable<String, Datum> taggedOutput) {

		ArrayList<Signature> oss = _pnode.getOutputSignature();
		String nodeName = _pnode.getNodeName();

		if (taggedOutput.size() != oss.size()) {
			throw new TransformerException("Number of values outputed by : "
					+ Pnode.substrIRI(_pnode.getRDFName())
					+ "does not match model");
		}

		for (Signature os : oss) {
			String sigPinTag = os.getSourcePin();
			Datum d = taggedOutput.get(sigPinTag);
			_umem.write(nodeName, sigPinTag, d);
		}

	}

	public void setMemoryObject(Record record) {
		this._umem = record.getUmem();
	}

	protected LinkedHashMap<String, Datum> iBuf() {
		return _iBuf;
	}

	protected Pnode pNode() {
		return _pnode;
	}

	protected Umem uMem() {
		return _umem;
	}
}
